import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login", //登录页
    name: "login",
    component: () =>
      import(/* webpackChunkName: "login" */ "../views/login/index.vue"),
  },
  {
    path: "/home", //布局页
    name: "home",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/home/index.vue"),
    children: [
      {
        path: "/home", //首页1
        name: "home",
        component: () =>
          import(/* webpackChunkName: "about" */ "../views/home/index2.vue"),
      },
      {
        path: "/index", //首页
        name: "index",
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/home/index/index.vue"
          ),
      },
      {
        path: "/search", //全局搜索页
        name: "search",
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/home/search.vue"
          ),
      },
      {
        path: "/number", //通讯录页面
        name: "number",
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/home/number.vue"
          ),
      },
      {
        path: "/abeyance", //代办事项页面
        name: "abeyance",
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/home/abeyance.vue"
          ),
      },
      {
        path: "/organ", //组织管理页
        name: "organ",
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/home/organ/index.vue"
          ),
        children: [
          {
            path: "/organ/bum", //部门管理页
            name: "organBum",
            component: () =>
              import(
                /* webpackChunkName: "about" */ "../views/home/organ/bum.vue"
              ),
          },
        ],
      },
      {
        path: "/organ", //组织管理页
        name: "organ",
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/home/organ/index.vue"
          ),
        children: [
          {
            path: "/organ/jse", //角色管理页
            name: "organJse",
            component: () =>
              import(
                /* webpackChunkName: "about" */ "../views/home/organ/jse.vue"
              ),
          },
        ],
      },

      {
        path: "/organ", //组织管理页
        name: "organ",
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/home/organ/index.vue"
          ),
        children: [
          {
            path: "/organ/yhu", //用户管理页
            name: "organYhu",
            component: () =>
              import(
                /* webpackChunkName: "about" */ "../views/home/organ/yhu.vue"
              ),
          },
        ],
      },
      {
        path: "/cloud", //云办公页
        name: "cloud",
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/home/cloud/index.vue"
          ),
        children: [
          {
            path: "/cloud/shpi", //审批管理页
            name: "cloudShpi",
            component: () =>
              import(
                /* webpackChunkName: "about" */ "../views/home/cloud/shpi.vue"
              ),
          },
        ],
      },

      {
        path: "/cloud", //云办公页
        name: "cloud",
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/home/cloud/index.vue"
          ),
        children: [
          {
            path: "/cloud/qjia", //请假管理页
            name: "cloudQjia",
            component: () =>
              import(
                /* webpackChunkName: "about" */ "../views/home/cloud/qjia.vue"
              ),
          },
        ],
      },

      {
        path: "/cloud", //云办公页
        name: "cloud",
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/home/cloud/index.vue"
          ),
        children: [
          {
            path: "/cloud/baox", //报销管理页
            name: "cloudBaox",
            component: () =>
              import(
                /* webpackChunkName: "about" */ "../views/home/cloud/baox.vue"
              ),
          },
        ],
      },
      {
        path: "/cloud", //云办公页
        name: "cloud",
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/home/cloud/index.vue"
          ),
        children: [
          {
            path: "/cloud/bus", //部署管理页
            name: "cloudBus",
            component: () =>
              import(
                /* webpackChunkName: "about" */ "../views/home/cloud/bus.vue"
              ),
          },
        ],
      },
      {
        path: "/meet", //会议管理页
        name: "meet",
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/home/meet/index.vue"
          ),
      },

      {
        path: "/person", //人事管理页
        name: "person",
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/home/person/index.vue"
          ),
        children: [
          {
            path: "/person/kao", //考勤管理页
            name: "personKao",
            component: () =>
              import(
                /* webpackChunkName: "about" */ "../views/home/person/kao.vue"
              ),
          },
        ],
      },
      {
        path: "/person", //人事管理页
        name: "person",
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/home/person/index.vue"
          ),
        children: [
          {
            path: "/person/gzi", //人事管理页
            name: "personGzi",
            component: () =>
              import(
                /* webpackChunkName: "about" */ "../views/home/person/gzi.vue"
              ),
          },
        ],
      },
      {
        path: "/person", //人事管理页
        name: "person",
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/home/person/index.vue"
          ),
        children: [
          {
            path: "/person/jix", //人事管理页
            name: "personJix",
            component: () =>
              import(
                /* webpackChunkName: "about" */ "../views/home/person/jix.vue"
              ),
          },
        ],
      },
      {
        path: "/announce", //公告页
        name: "announce",
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/home/announce/index.vue"
          ),
      },
    ],
  },
];

const router = new VueRouter({
  routes,
});

//挂载路由导航守卫
router.beforeEach((to, from, next) => {
  //   //to将要访问的路径
  //   //from 代表从哪个路径跳转而来
  //   //next是一个函数，表示发行
  //   //next() 发行 next("/login")强制跳转

  //如果用户跳转到登录页直接发行
  if (to.path === "/login") return next();
  //获取保存在sessionStorage中的token值
  const tokenStr = window.sessionStorage.getItem("token");
  //如果没有token值强制跳转到登录页
  if (!tokenStr) return next("/login");
  next();
});

export default router;
